package user

import (
	"chain_bridge_service/internal/code"
	"chain_bridge_service/internal/pkg/core"
	"chain_bridge_service/internal/pkg/validation"
	"chain_bridge_service/internal/services/user"
	"net/http"
)

type UpdateUserData struct {
	Id           string `bson:"_id,omitempty" json:"id"`
	UserName     string `bson:"userName" json:"userName"`
	Password     string `bson:"password" json:"password"`
	Email        string `bson:"email" json:"email"`
	MaturityTime int64
	Remark       string
}

func (h *handler) Update() core.HandlerFunc {
	return func(ctx core.Context) {
		req := new(UpdateUserData)
		res := new(createResponse)
		if err := ctx.ShouldBindJSON(req); err != nil {
			ctx.AbortWithError(core.Error(
				http.StatusOK,
				code.ParamBindError,
				validation.Error(err)).WithError(err),
			)
			return
		}
		data := new(user.CreateData)
		data.UserName = req.UserName
		data.Password = req.Password
		data.Email = req.Email
		data.Id = req.Id
		data.MaturityTime = req.MaturityTime
		data.Remark = req.Remark
		err := h.userService.Update(ctx, data)
		if err != nil {
			ctx.AbortWithError(core.Error(
				http.StatusOK,
				code.UpdateUserError,
				code.Text(code.UpdateUserError)).WithError(err),
			)
			return
		}

		res.Code = code.StatusOK
		res.Data = nil
		res.Msg = code.Text(code.StatusOK)
		ctx.Payload(res)
	}
}
